#!/bin/bash

npm run build:prod #打包正式服代码
tar zcvf dist.tar.gz dist #压缩

remoteDir='/opt/frontapps/grace-manage-admin'
nginxPath='/opt/apps/nginx/nginx-current/sbin/nginx'
nginxNodes='
root@192.168.0.1
'

function show_error()
{
    (($1 != 0)) && echo "${2}失败..." || echo "${2}成功..."
}


#文件上传
for m in ${nginxNodes}; do
	ssh "${m}" <<EOF
    mkdir -p $remoteDir
EOF
	scp dist.tar.gz "${m}":$remoteDir

	show_error $? "${m}-文件上传"
done

#解压删除文件
for m in ${nginxNodes}; do
	ssh "${m}" <<EOF
    cd $remoteDir
    rm -rf dist
    tar xvf dist.tar.gz
EOF
	show_error $? "${m}-文件解压"
done

#重启nginx#
for m in ${nginxNodes}; do
	ssh "${m}" <<EOF
	$nginxPath -s reload
EOF
	show_error $? "${m}-重启"
done
